// subpages0/giftCard/cardhome/cardhome.js
var app = getApp();
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    if (e && e.title) {
      app.setNavtitle(e.title);
    } else {
      app.setNavtitle('收到卡片');
    }
    if (e && e.id) {
      that.setData({
        id: e.id
      })
    }

    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.requestCarddetail();
        that.getSlient();
      }
    } else {
      that.requestCarddetail();
      that.getSlient();
    }
  },
  onShow: function () {

  },
  getSlient: function () {
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient : '';
    that.setData({
      slient: slient
    })
  },
  hideGetuser: function () {
    var that = this;
    that.setData({
      isShowgetuser: false
    })
  },
  // 获取用户信息
  getuserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      var userdata = e.detail;
      console.log(userdata);
      var slient = that.data.slient;
      var again = false;
      if (slient == 0) {
        again = true;
      }
      app.getuserInfo(that, userdata, again);
    }
  },
  onPullDownRefresh: function () {
    var that = this;
    that.requestCarddetail();
  },
  requestCarddetail: function () {
    var that = this;
    var data = {
      map: 'applet_giftcard_present_detail',
      id: that.data.id
    }
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            cardDetail: res.data.data
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  confirmGetcard:function(){
    var that = this;
    if (app.globalData.slient == 1) {
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定领取吗？',
      success(res) {
        if (res.confirm) {
          that.requestGetcard();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  requestGetcard: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_giftcard_active_card',
        id: that.data.id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg,
            showCancel:false,
            confirmText:'查看卡包',
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/subpages0/giftCard/cardBag/cardBag',
                })
              }
            }
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
})